<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
    <th:block th:include="include :: header('修改newstrends')" />
    <th:block th:include="include :: summernote-css" />
    <th:block th:include="include :: bootstrap-fileinput-css" />
    <th:block th:include="include :: datetimepicker-css" />
</head>
<body class="white-bg">
    <div class="wrapper wrapper-content animated fadeInRight ibox-content">
        <form class="form-horizontal m" id="form-websitenewstrends-edit" th:object="${websiteNewsTrends}">
            <input name="newsId" id="newsId" th:field="*{newsId}" type="hidden">
            <div class="form-group">    
                <label class="col-sm-3 control-label">新闻名称：</label>
                <div class="col-sm-8">
                    <input name="newsName" id="newsName" th:field="*{newsName}" class="form-control" type="text">
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label">新闻图片：</label>
                <div class="col-sm-8">
                    <div class="form-group">
                        <div class="file-loading">
                            <input id="file" type="file" multiple name="newsPhoto" class="form-control">
                        </div>
                    </div>
                    <input name="newsPhoto" id="newsPhoto" th:field="*{newsPhoto}" class="form-control" type="hidden">
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label">新闻内容：</label>
                <div class="col-sm-8">
                    <input name="newsContent" id="newsContent" th:field="*{newsContent}" class="form-control" type="hidden">
                    <div class="summernote" >

                    </div>
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label">附件内容：</label>
                <div class="col-sm-8">
                    <div class="form-group">
                        <div class="file-loading">
                            <input id="fileinput-demo-1" type="file" multiple data-theme="fas" data-show-caption="true">
                        </div>
                    </div>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label">发布日期：</label>
                <div class="col-sm-8">
                    <input name="createTime" id="createTime" type="text" class="form-control" th:value="${#dates.format(websiteNewsTrends.createTime,'yyyy-MM-dd HH:mm:ss')}">
                </div>
            </div>

            <input name="ifShow" id="ifShow" type="hidden" th:field="*{ifShow}">
        </form>
        <div style="text-align: center">
            <a class="btn btn-primary  btn-xl" onclick="submitHandler()"><i class="fa fa-search"></i>&nbsp;保存</a>
            <a class="btn btn-default  btn-xl" onclick="$.modal.reload()"><i class="fa fa-close"></i>&nbsp;关闭</a>
        </div>
    </div>
    <th:block th:include="include :: footer" />
    <th:block th:include="include :: summernote-js" />
    <th:block th:include="include :: bootstrap-fileinput-js" />
    <th:block th:include="include :: datetimepicker-js" />
    <script type="text/javascript">
        var prefix = ctx + "website/websitenewstrends";
        $("#form-websitenewstrends-edit").validate({
            focusCleanup: true
        });

        function submitHandler() {
            if ($.validate.form()) {
                //$.operate.save(prefix + "/add", $('#form-websitenewstrends-add').serialize());
                let formData = new FormData();
                formData.append("newsId",$("#newsId").val());
                formData.append("newsName",$("#newsName").val());
                formData.append("newsContent",$('.summernote').summernote("code"));
                if ($("#file").val()!=null || $("#file").val!=undefined) {
                    formData.append("files", $("#file")[0].files[0]);
                }
                for(var i=0;i<$("#fileinput-demo-1")[0].files.length;i++){
                    formData.append("files", $("#fileinput-demo-1")[0].files[i]);
                }
                formData.append("createTime",$("#createTime").val());
                console.log(formData.get("files"))
                debugger;
                $.ajax({
                    type: 'post',
                    url: prefix + '/edit',
                    data : formData,
                    cache: false,
                    contentType: false,
                    processData: false,
                    dataType: 'json',
                    success: function(result) {
                        if(result.code==0){
                            $.modal.msgReload(result.msg,"success");
                        }else{
                            $.modal.msgReload(result.msg,"error");
                        }
                    },
                    error: function(error) {
                        $.modal.alertWarning("修改失败");
                    }
                })
            }
        }

        $(function(){
            //初始化sumernote插件
            $('.summernote').summernote({
                placeholder: '请输入展示内容主体',
                height : 520,
                lang : 'zh-CN',
                followingToolbar: false,
                width: 820,
                callbacks: {
                    onImageUpload: function(files, editor, $editable) {
                        var formData = new FormData();
                        formData.append("file", files[0]);
                        $.ajax({
                            type: "POST",
                            url: ctx + "common/upload",
                            data: formData,
                            cache: false,
                            contentType: false,
                            processData: false,
                            dataType: 'json',
                            success: function(result) {
                                if (result.code == web_status.SUCCESS) {
                                    //alert(result.url+","+result.fileName)
                                    $('.summernote').summernote('editor.insertImage', result.url, result.fileName);
                                } else {
                                    $.modal.alertError(result.msg);
                                }
                            },
                            error: function(error) {
                                $.modal.alertWarning("图片上传失败。");
                            }
                        });
                    }
                }
            });
            //初始化summernote的内容
            $('.summernote').summernote('code',$("#newsContent").val());
            //初始化时间插件
            $("#createTime").datetimepicker({
                format: "yyyy-mm-dd hh:ii:ss",
                startView: 2,
                minView: "month",
                autoclose: true
            })
            //初始化文件上传插件
            $("#fileinput-demo-1").fileinput({
                'theme': 'explorer-fas',
                allowedFileExtensions: ['doc', 'docx', 'xls', 'xlsx', 'ppt', 'pptx', 'zip', 'rar', "html", "htm", 'txt', 'pdf'],//接收的文件后缀,
                enctype: 'multipart/form-data',
                language: 'zh',
                overwriteInitial: false,
                initialPreviewAsData: true,
                contentType: false,
                showUpload: false,
                showBrowse: true,
                showPreview: true,
                showRemove: true,
                showCancel:true,
                previewFileType:['html', 'text', 'video', 'audio', 'flash', 'object'],
                dropZoneTitle: "请上传附件,上传附件会替换掉之前所有附件。不重新上传仍使用之前附件。",
            });
            //初始化图片上传插件
            $('#file').fileinput({
                allowedFileExtensions: ['jpg', 'png', 'gif'],//接收的文件后缀,
                enctype: 'multipart/form-data',
                showBrowse: true,
                showUpload: false, //是否显示上传按钮
                showCaption: false,//是否显示标题
                showRemove: false,
                browseClass: "btn btn-success", //按钮样式
                previewFileIcon: "<i class='glyphicon glyphicon-king'></i>",
                initialPreviewAsData: true,
                initialPreviewFileType: 'image',
                dropZoneTitle: "请上传新闻图片，该图片用于轮播图展示",
                initialPreview: ctx + $("#newsPhoto").val(), //要显示的图片的路径
                //msgFilesTooMany: "选择上传的文件数量({2}) 超过允许的最大数值{2}！",
                layoutTemplates :{
                    actionDelete:'', //去除上传预览的缩略图中的删除图标
                },
            });
        })
    </script>
</body>
</html>